/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.mobiubiq.directory.client.frame.service;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import org.kalmeo.kuix.core.Kuix;
import org.kalmeo.kuix.core.KuixConstants;
import org.kalmeo.kuix.widget.Desktop;
import org.kalmeo.kuix.widget.Screen;
import org.kalmeo.kuix.widget.Text;
import org.kalmeo.util.frame.FrameHandler;
import org.mobiubiq.directory.client.KuixClient;
import org.mobiubiq.directory.client.data.Service;
import org.mobiubiq.directory.client.frame.AbstractBaseFrame;
import org.mobiubiq.directory.client.widget.Camera;
import org.mobiubiq.directory.client.widget.Form;
import org.mobiubiq.directory.client.ws.WebServiceAction;
import org.mobiubiq.directory.client.ws.WebServiceClient;
import org.mobiubiq.directory.client.ws.WebServiceException;

/**
 *
 * @author Marnix Renne
 */
public class AppServiceFrame implements ServiceHandlerFrame {

    public static final String PROTOCOL = "application";
    private Service service;
    private Screen screen;
    private WebServiceClient webServiceClient;

    public AppServiceFrame(Service service) {
        super();
        this.service = service;
        webServiceClient = new WebServiceClient();
    }

    public Service getService() {
        return service;
    }

    public String getProtocol() {
        return PROTOCOL;
    }

    public boolean onMessage(Object identifier, Object[] arguments) {
        if ("back".equals(identifier)) {
            FrameHandler frameHandler = Kuix.getFrameHandler();
            frameHandler.removeFrame(this);
            AbstractBaseFrame frame = (AbstractBaseFrame) frameHandler.getTopFrame();
            frame.showScreen();
            System.out.println(frame.toString());
            return false;
        }
        if ("submit".equals(identifier)) {
            try {
                Desktop desktop = Kuix.getCanvas().getDesktop();
                Form form = (Form) desktop.getWidget("form");
                WebServiceAction action = new WebServiceAction(service, form.getAction());
                Enumeration e = form.getInputs().elements();
                while (e.hasMoreElements()) {
                    Text textWidget = (Text) e.nextElement();
                    String text = textWidget.getText();
                    if (text == null) {
                        text = "";
                    }
                    action.addQueryParameter(textWidget.getId(), text);
                }
                InputStream inputStream = webServiceClient.doGetRequest(service, action);
                screen = Kuix.loadScreen(inputStream, null);
                screen.setCleanUpWhenRemoved(true);
                screen.setCurrent();
            } catch (Throwable t) {
                Kuix.alert(t.getMessage(), KuixConstants.ALERT_ERROR);
                FrameHandler handler = Kuix.getFrameHandler();
                handler.removeFrame(this);
                AbstractBaseFrame topframe = (AbstractBaseFrame) handler.getTopFrame();
                topframe.showScreen();                  
            } finally {
                return false;
            }
        }
        if ("takeSnapshot".equals(identifier)) {
            InputStream inputStream = null;
            try {
                Desktop desktop = Kuix.getCanvas().getDesktop();
                Camera camera = (Camera) desktop.getWidget("camera");
                byte[] image = camera.takeSnapshot();
                camera.cleanUp();
                screen.remove();
                WebServiceAction action = new WebServiceAction(service, "post");
                action.addPostParameter("imageData", image);
                action.addQueryParameter("description", "picture of object");
                action.addQueryParameter("tags", "mobiubiq");
                inputStream = webServiceClient.doPostRequest(service, action);
                screen = Kuix.loadScreen(inputStream, null);
                screen.setCleanUpWhenRemoved(true);
                screen.setCurrent();
            } catch (WebServiceException ex) {
                ex.printStackTrace();
            } finally {
                try {
                    inputStream.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                return false;
            }
        }
        return true;
    }

    public void onAdded() {
        try {
            KuixClient.showProcessingScreen(Kuix.getMessage("PROCESSING_MESSAGE"));
            Thread t = new Thread(new Runnable() {
                public void run() {
                    InputStream inputStream = null;
                    try {
                        WebServiceAction action = new WebServiceAction(service, "init");
                        inputStream = webServiceClient.doGetRequest(service, action);
                        KuixClient.cleanupProcessingScreen();
                        screen = Kuix.loadScreen(inputStream, null);
                        screen.setCleanUpWhenRemoved(true);
                        screen.setCurrent();
                    } catch (WebServiceException ex) {
                        // TODO: proper exception handling
                        Kuix.alert(ex);
                        Kuix.getFrameHandler().removeFrame(AppServiceFrame.this);
                    } finally {
                        try {
                            inputStream.close();
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }
                }
            });
            t.start();
        } catch (Throwable t) {
            Kuix.alert(t);
            Kuix.getFrameHandler().removeFrame(this);
        }
    }

    public void onRemoved() {
        screen.cleanUp();
        screen = null;
        service = null;
    }
}
